package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.account.group.AccountGroupDto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.security.MyClaims;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.AccountGroupService;
import com.opennews.openplatform.service.IncomeExpenseDetailService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/account-group")
public class AccountGroupController {
    private final AccountGroupService accountGroupService;
    private final IncomeExpenseDetailService incomeExpenseDetailService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/query-account-group-info")
    public ResponseEntity<ApiResponse<Map<String, Object>>> queryAccountGroupInfo() {
        MyClaims claims = requestIdentityExtractor.getClaims();
        String accountGroupId = claims.accountGroupId;
        String userId = claims.id;
        Map<String, Object> result = accountGroupService.queryAccountGroupInfo(accountGroupId, userId);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @GetMapping("/query-basic-statistics")
    public ResponseEntity<ApiResponse<Map<String, Object>>> queryBasicStatistics() {
        MyClaims claims = requestIdentityExtractor.getClaims();
        String accountGroupId = claims.accountGroupId;
        Map<String, Object> result = incomeExpenseDetailService.queryBasicStatistics(accountGroupId);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/update")
    public ResponseEntity<ApiResponse<Object>> update(@Valid @RequestBody AccountGroupDto data) {
        accountGroupService.update(data);

        return httpResponseHelper.getSuccessResponse();
    }
}
